<template>
  <div class="comment-section">
    <!-- 标题 -->
    <div class="header">
      <h3><el-icon><ChatDotRound /></el-icon>评论 ({{ comments.length }})</h3>
      <p>分享你的想法，参与讨论</p>
    </div>
    
    <!-- 评论表单 -->
    <div class="comment-form">
      <el-avatar :size="40" :src="currentUser.avatar" />
      <div class="form-content">
        <el-input
          v-model="newComment"
          type="textarea"
          :rows="2"
          placeholder="写下你的精彩评论..."
          maxlength="200"
          show-word-limit
        />
        <el-button
          type="primary"
          :disabled="!newComment.trim()"
          @click="submitComment"
        >
          发布评论
        </el-button>
      </div>
    </div>
    
    <!-- 评论列表 -->
    <div class="comment-list">
      <div v-for="comment in comments" :key="comment.id" class="comment-item">
        <el-avatar :size="40" :src="comment.avatar" />
        <div class="content">
          <div class="meta">
            <span class="author">{{ comment.author }}</span>
            <span class="badge">Verified</span>
            <span class="time">{{ comment.time }}</span>
          </div>
          <p class="text">{{ comment.content }}</p>
          <div class="actions">
            <button @click="likeComment(comment.id)">
              <el-icon><Star /></el-icon> {{ comment.likes }}
            </button>
            <button>
              <el-icon><ChatDotRound /></el-icon> 回复
            </button>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 空状态 -->
    <div v-if="!comments.length" class="empty">
      <el-icon><ChatLineRound /></el-icon>
      <p>还没有评论，快来发表第一条吧！</p>
    </div>

    <!-- 验证码弹窗 -->
    <Captcha
      v-model:visible="showCaptcha"
      code-url="http://ll.cn/api/code/index"
      @confirm="confirmCaptcha"
    />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { Star, ChatDotRound, ChatLineRound } from '@element-plus/icons-vue'
import Captcha from '@/components/Captcha.vue'

const props = defineProps({
  comments: { type: Array, default: () => [] }
})

const emit = defineEmits(['add-comment'])

const newComment = ref('')
const currentUser = ref({
  avatar: 'https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?w=150&h=150&fit=crop&crop=face'
})

const showCaptcha = ref(false)

const submitComment = () => {
  if (!newComment.value.trim()) return
  showCaptcha.value = true  // Show CAPTCHA before submitting
}

const confirmCaptcha = (captchaCode) => {
  console.log('CAPTCHA验证成功:', captchaCode)
  emit('add-comment', newComment.value)
  newComment.value = ''
  showCaptcha.value = false  // Close modal
}

const likeComment = (id) => console.log('点赞:', id)
</script>

<style scoped>
.comment-section {
  max-width: 1000px;
  margin: 0 auto;
  padding: 0;
}

.header {
  text-align: center;
  margin-bottom: 30px;
}

.header h3 {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 20px;
  color: #1a1a1a;
  margin: 0 0 8px 0;
}

.header p {
  color: #666;
  margin: 0;
  font-size: 14px;
}

.comment-form {
  display: flex;
  gap: 16px;
  margin-bottom: 30px;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 12px;
}

.form-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.form-content :deep(.el-textarea__inner) {
  border-radius: 8px;
  min-height: 60px;
  resize: none;
}

.comment-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.comment-item {
  display: flex;
  gap: 16px;
  padding: 16px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}

.content {
  flex: 1;
}

.meta {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}

.author {
  font-weight: 600;
  color: #1a1a1a;
}

.badge {
  background: #409eff;
  color: white;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
}

.time {
  color: #999;
  font-size: 13px;
  margin-left: auto;
}

.text {
  color: #333;
  line-height: 1.5;
  margin: 0 0 12px 0;
}

.actions {
  display: flex;
  gap: 16px;
}

.actions button {
  display: flex;
  align-items: center;
  gap: 4px;
  background: none;
  border: none;
  color: #666;
  cursor: pointer;
  font-size: 13px;
  padding: 4px 8px;
  border-radius: 4px;
  transition: all 0.2s;
}

.actions button:hover {
  background: #f5f5f5;
  color: #409eff;
}

.empty {
  text-align: center;
  padding: 40px 20px;
  color: #999;
}

.empty .el-icon {
  font-size: 48px;
  margin-bottom: 12px;
}

.empty p {
  margin: 0;
  font-size: 14px;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .comment-section {
    padding: 16px;
  }
  
  .comment-form {
    flex-direction: column;
    padding: 16px;
    gap: 12px;
  }
  
  .comment-item {
    flex-direction: column;
    gap: 12px;
    padding: 12px;
  }
  
  .meta {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
  
  .time {
    margin-left: 0;
  }
  
  .actions {
    gap: 12px;
  }
}
</style>